package com.example.bailin.giftsay.main.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.entity.SelectPopupEntity;
import com.example.bailin.giftsay.main.SXpupListener;
import com.example.bailin.giftsay.tool.MyApp;

/**
 * Created by bailin on 16/8/24.
 * 选礼神器筛选的popupwindow的适配器
 */
public class SelectGiftAdapter extends RecyclerView.Adapter<SelectGiftAdapter.SelectGiftViewHolder> {
    private SelectPopupEntity entity;
    private SXpupListener sXpupListener;
    private int index;
    private int select;

    public void setsXpupListener(SXpupListener sXpupListener) {
        this.sXpupListener = sXpupListener;
    }


    public void setEntity(SelectPopupEntity entity) {
        this.entity = entity;
        for (SelectPopupEntity.DataBean.FiltersBean filtersBean : entity.getData().getFilters()) {
            SelectPopupEntity.DataBean.FiltersBean.ChannelsBean channelsBean = new SelectPopupEntity.DataBean.FiltersBean.ChannelsBean();
            channelsBean.setName("全部");
            filtersBean.getChannels().add(0, channelsBean);
        }
        notifyDataSetChanged();
    }

    public void setIndex(int index) {
        this.index = index;
        notifyDataSetChanged();
    }

    @Override
    public SelectGiftViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(MyApp.getContext()).inflate(R.layout.item_selectgift, parent, false);
        SelectGiftViewHolder viewHolder = new SelectGiftViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final SelectGiftViewHolder holder, final int position) {
        holder.tv.setText(entity.getData().getFilters().get(index).getChannels().get(position).getName());
        if (position == select) {
            holder.tv.setTextColor(0xffffffff);
            holder.tv.setBackgroundColor(0XFFF43E5E);
        } else {
            holder.tv.setTextColor(Color.BLACK);
            holder.tv.setBackgroundColor(0Xffffffff);
        }
        holder.rl.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                sXpupListener.onClick(position, index,
                        entity.getData().getFilters().get(index).getKey(),
                        entity.getData().getFilters().get(index).getChannels().get(position).getKey(),
                        entity.getData().getFilters().get(index).getChannels().get(position).getName());

            }
        });
    }

    @Override
    public int getItemCount() {
        return entity.getData().getFilters().get(index).getChannels().size();
    }

    public void setSelect(int select) {
        this.select = select;
    }

    public class SelectGiftViewHolder extends RecyclerView.ViewHolder {
        TextView tv;
        RelativeLayout rl;

        public SelectGiftViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv_item_selectgift);
            rl = (RelativeLayout) itemView.findViewById(R.id.rl_item_xlsq_sx);
        }
    }
}
